package ua.khuta.myskidka;


import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class FragmentFilterNews extends Fragment implements OnClickListener {
	View v;
	CheckBox cbAvto, cbOdezdaMyz, cbOdezdaZen, cbOdezdaDeti, cbSalonKrasoti,
			cbBariRestorani, cbDosygOtdih, cbProdyctiPitaniya;
	Database db;
	Button btnSaveChanges;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fragment_filter_news, null);
		initialization(v);
		db = Database.getInstance(getActivity());
		Cursor c = db.getCategoriesFromDb();
		Log.d("myLogs", "count=" + c.getCount());
		if (c != null) {
			if (c.moveToFirst()) {
				int i = 0;
				do {
					int newsFlag = c.getInt(3);
					Log.d("myLogs", i + ". =" + newsFlag);
					switch (i) {
					case 0:
						if (newsFlag == 1)
							cbAvto.setChecked(true);
						break;
					case 1:
						if (newsFlag == 1)
							cbOdezdaMyz.setChecked(true);
						break;
					case 2:
						if (newsFlag == 1)
							cbOdezdaZen.setChecked(true);
						break;
					case 3:
						if (newsFlag == 1)
							cbOdezdaDeti.setChecked(true);
						break;
					case 4:
						if (newsFlag == 1)
							cbSalonKrasoti.setChecked(true);
						break;
					case 5:
						if (newsFlag == 1)
							cbBariRestorani.setChecked(true);
						break;
					case 6:
						if (newsFlag == 1)
							cbDosygOtdih.setChecked(true);
						break;
					case 7:
						if (newsFlag == 1)
							cbProdyctiPitaniya.setChecked(true);
						break;
					default:
						break;
					}
					i++;

				} while (c.moveToNext());
			} else {
				Log.d("myLogs", "cursor  is NULL in move to first");
			}
		} else
			Log.d("myLogs", "cursor is NULL");
		return v;
	}

	public void initialization(View v) {
		cbAvto = (CheckBox) v.findViewById(R.id.checkBox1);

		cbOdezdaMyz = (CheckBox) v.findViewById(R.id.checkBox2);
		cbOdezdaZen = (CheckBox) v.findViewById(R.id.checkBox3);
		cbOdezdaDeti = (CheckBox) v.findViewById(R.id.checkBox4);
		cbSalonKrasoti = (CheckBox) v.findViewById(R.id.checkBox5);
		cbBariRestorani = (CheckBox) v.findViewById(R.id.checkBox6);
		cbDosygOtdih = (CheckBox) v.findViewById(R.id.checkBox7);
		cbProdyctiPitaniya = (CheckBox) v.findViewById(R.id.checkBox8);
		btnSaveChanges = (Button) v.findViewById(R.id.btn_save_changes);
		btnSaveChanges.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_save_changes:
			if (cbAvto.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 1, null);
				
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 1, null);
			}

			if (cbBariRestorani.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 2, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 2, null);
			}

			if (cbDosygOtdih.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 7, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 7, null);
			}

			if (cbOdezdaDeti.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 4, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 4, null);
			}

			if (cbOdezdaMyz.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 2, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 2, null);
			}

			if (cbOdezdaZen.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 3, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 3, null);
			}

			if (cbProdyctiPitaniya.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 8, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 8, null);
			}

			if (cbSalonKrasoti.isChecked()) {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 1);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 5, null);
			} else {
				ContentValues cv = new ContentValues();
				cv.put(Database.CATEGORY_IN_NEWS, 0);
				db.mDB.update(Database.DB_TABLE_CATEGORY, cv,
						"_id = " + 5, null);
			}
			break;

		default:
			break;
		}

	}
}
